package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.Member;
import com.powernode.model.Result;
import com.powernode.service.MemberService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @create: 2024/7/20
 * @author: gyhe10
 * @description: 微信小程序会员业务控制层
 **/

@Api(tags = "微信小程序会员业务接口管理")
@RestController
@RequestMapping("p/user")
public class MemberController {

    @Resource
    private MemberService memberService;

    /**
     * 更新会员头像和昵称
     * @param member 会员对象
     * @return
     */
    @ApiOperation("更新会员头像和昵称")
    @PutMapping("setUserInfo")
    public Result<String> modifyUserInfo(@RequestBody Member member) {
        // 获取当前登录会员的openId
        String openId = AuthUtils.getMemberOpenId();
        // 修改会员信息
        boolean modified = memberService.update(member, new LambdaQueryWrapper<Member>()
                .eq(Member::getOpenId, openId));
        return Result.handle(modified);
    }

    /**
     * 查询会员是否绑定手机号
     * @return
     */
    @ApiOperation("查询会员是否绑定手机号")
    @GetMapping("isBindPhone")
    public Result<Boolean> loadMemberIsBindPhone() {
        // 获取当前登录会员的openId
        String openId = AuthUtils.getMemberOpenId();
        // 根据openId查询会员信息
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .eq(Member::getOpenId, openId));
        return Result.success(StringUtils.hasText(member.getUserMobile()));
    }


    //////////////// feign 接口 ////////////////

    @GetMapping("getMemberListByOpenIdList")
    public Result<List<Member>> getMemberListByOpenIdList(@RequestParam List<String> openIdList) {
        List<Member> memberList = memberService.list(new LambdaQueryWrapper<Member>()
                .in(Member::getOpenId, openIdList));
        return Result.success(memberList);
    }

}
